home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / GSDEVICE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  16.8 KB  |  579 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsdevice.c */
  21. /* Device operators for Ghostscript library */
  22. #include "math_.h"            /* for fabs */
  23. #include "memory_.h"            /* for memcpy */
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gsprops.h"
  27. #include "gsutil.h"
  28. #include "gxarith.h"
  29. #include "gxfixed.h"            /* ditto */
  30. #include "gxmatrix.h"            /* for gzstate.h */
  31. #include "gzstate.h"
  32. #include "gzdevice.h"
  33. #include "gxdevmem.h"
  34.  
  35. /* Import the device list from gdevs.c */
  36. extern gx_device *gx_device_list[];
  37.  
  38. /* Device definitions */
  39. /* Following defines the null device */
  40. private dev_proc_fill_rectangle(null_fill_rectangle);
  41. private dev_proc_tile_rectangle(null_tile_rectangle);
  42. private dev_proc_copy_mono(null_copy_mono);
  43. private dev_proc_draw_line(null_draw_line);
  44.  
  45. private gx_device_procs null_procs = {
  46.     gx_default_open_device,
  47.     gx_default_get_initial_matrix,
  48.     gx_default_sync_output,
  49.     gx_default_output_page,
  50.     gx_default_close_device,
  51.     gx_default_map_rgb_color,
  52.     gx_default_map_color_rgb,
  53.     null_fill_rectangle,
  54.     null_tile_rectangle,
  55.     null_copy_mono,
  56.     gx_default_copy_color,
  57.     null_draw_line,
  58.     gx_default_get_bits,
  59.     gx_default_get_props,
  60.     gx_default_put_props
  61. };
  62. private gx_device null_device = {
  63.     sizeof(device),
  64.     &null_procs,
  65.     "null",
  66.     0, 0,
  67.     72, 72,
  68.     no_margins,
  69.     dci_black_and_white,
  70.     1
  71. };
  72.  
  73. /* The null device */
  74. gx_device *gx_device_null_p = &null_device;
  75.  
  76. /* Flush buffered output to the device */
  77. int
  78. gs_flushpage(gs_state *pgs)
  79. {    gx_device *dev = pgs->device->info;
  80.     return (*dev->procs->sync_output)(dev);
  81. }
  82.  
  83. /* Make the device output the accumulated page description */
  84. int
  85. gs_copypage(gs_state *pgs)
  86. {    return gs_output_page(pgs, 1, 0);
  87. }
  88. int
  89. gs_output_page(gs_state *pgs, int num_copies, int flush)
  90. {    gx_device *dev = pgs->device->info;
  91.     return (*dev->procs->output_page)(dev, num_copies, flush);
  92. }
  93.  
  94. /* Copy scan lines from an image device */
  95. int
  96. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  97.   int *plines_copied, uint *pbytes_copied)
  98. {    uint line_size = gx_device_bytes_per_scan_line(dev, 0);
  99.     int code = (*dev->procs->get_bits)(dev, start_y, data, size, 0);
  100.     uint count;
  101.     if ( code < 0 ) return_error(gs_error_undefined);
  102.     count = size / line_size;
  103.     if ( plines_copied != NULL )
  104.       *plines_copied = count;
  105.     if ( pbytes_copied != NULL )
  106.       *pbytes_copied = count * line_size;
  107.     return 0;
  108. }
  109.  
  110. /* Get the current device from the graphics state */
  111. gx_device *
  112. gs_currentdevice(const gs_state *pgs)
  113. {    return pgs->device->info;
  114. }
  115.  
  116. /* Get the name of a device */
  117. const char *
  118. gs_devicename(const gx_device *dev)
  119. {    return dev->dname;
  120. }
  121.  
  122. /* Get the initial matrix of a device. */
  123. void
  124. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  125. {    (*dev->procs->get_initial_matrix)(dev, pmat);
  126. }
  127.  
  128. /* Get the N'th device from the known device list */
  129. gx_device *
  130. gs_getdevice(int index)
  131. {    int i;
  132.     for ( i = 0; gx_device_list[i] != 0; i++ )
  133.        {    if ( i == index ) return gx_device_list[i];
  134.        }
  135.     return 0;            /* index out of range */
  136. }
  137.  
  138. /* Clone an existing device. */
  139. int
  140. gs_copydevice(gx_device **pnew_dev, const gx_device *dev, proc_alloc_t palloc)
  141. {    register gx_device *new_dev;
  142.     new_dev = (gx_device *)(*palloc)(1, dev->params_size, "gs_copydevice");
  143.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  144.     memcpy(new_dev, dev, dev->params_size);
  145.     new_dev->is_open = 0;
  146.     *pnew_dev = new_dev;
  147.     return 0;
  148. }
  149.  
  150. /* Make a memory (image) device. */
  151. /* If num_colors = -16, -24, or -32, this is a true-color device; */
  152. /* otherwise, num_colors is the number of elements in the palette */
  153. /* (2^N or 3*2^N). */
  154. int
  155. gs_makeimagedevice(gx_device **pnew_dev, const gs_matrix *pmat,
  156.   uint width, uint height, const byte *colors, int num_colors,
  157.   proc_alloc_t palloc)
  158. {    const gx_device_memory *old_dev;
  159.     register gx_device_memory *new_dev;
  160.     byte *bits;
  161.     int palette_size = num_colors;
  162.     int bpp = 1;
  163.     int pcount;
  164.     int bits_per_pixel;
  165.     float x_pixels_per_unit, y_pixels_per_unit;
  166.     ulong bitmap_size;
  167.     byte palette[256 * 3];
  168.     int has_color;
  169.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  170.     switch ( num_colors )
  171.        {
  172.     case 3*2:
  173.         palette_size = 2; bpp = 3;
  174.     case 2:
  175.         bits_per_pixel = 1; break;
  176.     case 3*4:
  177.         palette_size = 4; bpp = 3;
  178.     case 4:
  179.         bits_per_pixel = 2; break;
  180.     case 3*16:
  181.         palette_size = 16; bpp = 3;
  182.     case 16:
  183.         bits_per_pixel = 4; break;
  184.     case 3*256:
  185.         palette_size = 256; bpp = 3;
  186.     case 256:
  187.         bits_per_pixel = 8; break;
  188.     case -16:
  189.         bits_per_pixel = 16; palette_size = 0; break;
  190.     case -24:
  191.         bits_per_pixel = 24; palette_size = 0; break;
  192.     case -32:
  193.         bits_per_pixel = 32; palette_size = 0; break;
  194.     default:
  195.         return_error(gs_error_rangecheck);
  196.        }
  197.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  198.     if ( old_dev == 0 )        /* no suitable device */
  199.         return_error(gs_error_rangecheck);
  200.     pcount = palette_size * 3;
  201.     /* Check to make sure the palette contains white and black, */
  202.     /* and, if it has any colors, the six primaries. */
  203.     if ( bits_per_pixel <= 8 )
  204.        {    const byte *p;
  205.         byte *q;
  206.         int primary_mask = 0;
  207.         int i;
  208.         has_color = 0;
  209.         for ( i = 0, p = colors, q = palette;
  210.               i < palette_size; i++, q += 3
  211.             )
  212.            {    int mask = 1;
  213.             switch ( bpp )
  214.                {
  215.             case 1:            /* gray */
  216.                 q[0] = q[1] = q[2] = *p++;
  217.                 break;
  218.             default:        /* bpp == 3, colored */
  219.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  220.                 p += 3;
  221.                }
  222. #define shift_mask(b,n)\
  223.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  224.             shift_mask(q[0], 4);
  225.             shift_mask(q[1], 2);
  226.             shift_mask(q[2], 1);
  227. #undef shift_mask
  228.             primary_mask |= mask;
  229.             if ( q[0] != q[1] || q[0] != q[2] )
  230.                 has_color = 1;
  231.            }
  232.         switch ( primary_mask )
  233.            {
  234.         case 129:        /* just black and white */
  235.             if ( has_color )    /* color but no primaries */
  236.                 return_error(gs_error_rangecheck);
  237.         case 255:        /* full color */
  238.             break;
  239.         default:
  240.             return_error(gs_error_rangecheck);
  241.            }
  242.        }
  243.     else
  244.         has_color = 1;
  245.     /*
  246.      * The initial transformation matrix must map 1 user unit to
  247.      * 1/72".  Let W and H be the width and height in pixels, and
  248.      * assume the initial matrix is of the form [A 0 0 B X Y].
  249.      * Then the size of the image in user units is (W/|A|,H/|B|),
  250.      * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
  251.      * the number of pixels per inch is
  252.      * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
  253.      * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
  254.      * or 270 degree rotation, the size of the image in user
  255.      * units is (W/|B|,H/|A|), so the pixels per inch are
  256.      * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
  257.      * matrices.
  258.      */
  259.     if ( is_fzero2(pmat->xy, pmat->yx) )
  260.         x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
  261.     else if ( is_fzero2(pmat->xx, pmat->yy) )
  262.         x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
  263.     else
  264.         return_error(gs_error_undefinedresult);
  265.     /* All checks done, allocate the device. */
  266.     new_dev = (gx_device_memory *)(*palloc)(1, old_dev->params_size, "gs_makeimagedevice(device)");
  267.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  268.     *new_dev = *old_dev;
  269.     new_dev->initial_matrix = *pmat;
  270.     new_dev->width = width;
  271.     new_dev->height = height;
  272.     new_dev->x_pixels_per_inch = fabs(x_pixels_per_unit) * 72;
  273.     new_dev->y_pixels_per_inch = fabs(y_pixels_per_unit) * 72;
  274.     if ( !has_color )
  275.         new_dev->color_info.max_rgb = 0,
  276.         new_dev->color_info.dither_rgb = 0;
  277.     bitmap_size = gdev_mem_bitmap_size(new_dev);
  278.     if ( bitmap_size > max_uint )    /* can't allocate it! */
  279.         return_error(gs_error_limitcheck);
  280.     bits = (byte *)(*palloc)(1, (uint)bitmap_size + pcount,
  281.                  "gs_makeimagedevice(bits)");
  282.     if ( bits == 0 ) return_error(gs_error_VMerror);
  283.     new_dev->base = bits;
  284.     new_dev->invert = (palette[0] | palette[1] | palette[2] ? -1 : 0);    /* bogus */
  285.     new_dev->palette_size = palette_size;
  286.     new_dev->palette = bits + bitmap_size;
  287.     memcpy(new_dev->palette, palette, pcount);
  288.     new_dev->is_open = 0;
  289.     *pnew_dev = (gx_device *)new_dev;
  290.     return 0;
  291. }
  292.  
  293. /* Set the device in the graphics state */
  294. int
  295. gs_setdevice(gs_state *pgs, gx_device *dev)
  296. {    register device *pdev = pgs->device;
  297.     int was_open = dev->is_open;
  298.     int code;
  299.     /* Initialize the device */
  300.     if ( !was_open )
  301.        {    code = (*dev->procs->open_device)(dev);
  302.         if ( code < 0 ) return_error(code);
  303.         dev->is_open = 1;
  304.        }
  305.     /* Compute device white and black codes */
  306.     pdev->black = (*dev->procs->map_rgb_color)(dev, 0, 0, 0);
  307.     pdev->white = (*dev->procs->map_rgb_color)(dev, gx_max_color_value, gx_max_color_value, gx_max_color_value);
  308.     pdev->info = dev;
  309.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  310.         (code = gs_initclip(pgs)) < 0
  311.        )
  312.         return code;
  313.     if ( !was_open )
  314.         if ( (code = gs_erasepage(pgs)) < 0 )
  315.             return code;
  316.     return gx_remap_color(pgs);
  317. }
  318.  
  319. /* Select the null device.  This is just a convenience. */
  320. void
  321. gs_nulldevice(gs_state *pgs)
  322. {    gs_setdevice(pgs, gx_device_null_p);
  323. }
  324.  
  325. /* Close a device.  The client is responsible for ensuring that */
  326. /* this device is not current in any graphics state. */
  327. int
  328. gs_closedevice(gx_device *dev)
  329. {    int code = 0;
  330.     if ( dev->is_open )
  331.        {    code = (*dev->procs->close_device)(dev);
  332.         if ( code < 0 ) return_error(code);
  333.         dev->is_open = 0;
  334.        }
  335.     return code;
  336. }
  337.  
  338. /* Install enough of a null device to suppress graphics output */
  339. /* during the execution of stringwidth. */
  340. void
  341. gx_device_no_output(gs_state *pgs)
  342. {    pgs->device->info = &null_device;
  343. }
  344.  
  345. /* Just set the device without reinitializing. */
  346. /* (For internal use only.) */
  347. void
  348. gx_set_device_only(gs_state *pgs, gx_device *dev)
  349. {    pgs->device->info = dev;
  350. }
  351.  
  352. /* Compute the size of one scan line for a device, */
  353. /* with or without padding to a word boundary. */
  354. uint
  355. gx_device_bytes_per_scan_line(const gx_device *dev, int pad)
  356. {    ulong bits = (ulong)dev->width * dev->color_info.depth;
  357.     return (pad ?
  358.         (uint)((bits + 31) >> 5) << 2 :
  359.         (uint)((bits + 7) >> 3));
  360. }
  361.  
  362. /* Adjust the resolution for devices that only have a fixed set of */
  363. /* geometries, so that the apparent size in inches remains constant. */
  364. int
  365. gx_device_adjust_resolution(gx_device *dev,
  366.   int actual_width, int actual_height)
  367. {    double width_ratio = (double)actual_width / dev->width ;
  368.     double height_ratio = (double)actual_height / dev->height ;
  369.     double ratio = max(width_ratio, height_ratio);
  370.     dev->x_pixels_per_inch *= ratio;
  371.     dev->y_pixels_per_inch *= ratio;
  372.     dev->width = actual_width;
  373.     dev->height = actual_height;
  374.     return 0;
  375. }
  376.  
  377. /* ------ The null `device' ------ */
  378.  
  379. private int
  380. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  381.   gx_color_index color)
  382. {    return 0;
  383. }
  384. private int
  385. null_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  386.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
  387.   int px, int py)
  388. {    return 0;
  389. }
  390. private int
  391. null_copy_mono(gx_device *dev, const byte *data,
  392.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  393.   gx_color_index zero, gx_color_index one)
  394. {    return 0;
  395. }
  396. private int
  397. null_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  398.   gx_color_index color)
  399. {    return 0;
  400. }
  401.  
  402. /* ------ Default device procedures ------ */
  403.  
  404. int
  405. gx_default_open_device(gx_device *dev)
  406. {    return 0;
  407. }
  408.  
  409. void
  410. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  411. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  412.     pmat->xy = 0;
  413.     pmat->yx = 0;
  414.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  415.     pmat->tx = 0;
  416.     pmat->ty = dev->height;    /****** WRONG for devices with ******/
  417.                 /****** arbitrary initial matrix ******/
  418. }
  419.  
  420. int
  421. gx_default_sync_output(gx_device *dev)
  422. {    return 0;
  423. }
  424.  
  425. int
  426. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  427. {    return (*dev->procs->sync_output)(dev);
  428. }
  429.  
  430. int
  431. gx_default_close_device(gx_device *dev)
  432. {    return 0;
  433. }
  434.  
  435. gx_color_index
  436. gx_default_map_rgb_color(gx_device *dev,
  437.   gx_color_value r, gx_color_value g, gx_color_value b)
  438. {    /* Map values >= 1/2 to 1, < 1/2 to 0. */
  439.     return ((r | g | b) > gx_max_color_value / 2 ?
  440.         (gx_color_index)1 : (gx_color_index)0);
  441. }
  442.  
  443. int
  444. gx_default_map_color_rgb(gx_device *dev, gx_color_index color,
  445.   gx_color_value prgb[3])
  446. {    /* Map 1 to max_value, 0 to 0. */
  447.     prgb[0] = prgb[1] = prgb[2] = -(gx_color_value)color;
  448.     return 0;
  449. }
  450.  
  451. int
  452. gx_default_copy_color(gx_device *dev, const byte *data,
  453.   int data_x, int raster, gx_bitmap_id id,
  454.   int x, int y, int width, int height)
  455. {    return (*dev->procs->copy_mono)(dev, data, data_x, raster, id,
  456.         x, y, width, height, (gx_color_index)0, (gx_color_index)1);
  457. }
  458.  
  459. int
  460. gx_default_get_bits(gx_device *dev, int y,
  461.   byte *data, unsigned int size, int pad)
  462. {    return -1;
  463. }
  464.  
  465. /* Standard device properties */
  466.  
  467. private const gs_prop_item props_std[] = {
  468.         /* Following can be set, but will close and */
  469.         /* reopen the device if necessary. */
  470.     prop_def("HWResolution", prt_float_array),
  471.     prop_def("HWSize", prt_int_array),
  472.         /* Following cannot be set yet */
  473.     prop_def("InitialMatrix", prt_float_array),
  474.         /* Following cannot be set */
  475.     prop_def("Name", prt_string),
  476.         /* Slots for arrays */
  477.     prop_float, prop_float,
  478.     prop_int, prop_int,
  479.     prop_float, prop_float, prop_float, prop_float,
  480.       prop_float, prop_float
  481. };
  482.  
  483. /* Get standard properties. */
  484. int
  485. gx_default_get_props(register gx_device *dev, register gs_prop_item *plist)
  486. {    if ( plist != 0 )
  487.        {    register gs_prop_item *pi;
  488.         gs_matrix mat;
  489.         memcpy(plist, props_std, sizeof(props_std));
  490.         plist[0].value.a.size = 2;
  491.         plist[1].value.a.size = 2;
  492.         plist[2].value.a.size = 6;
  493.         plist[3].value.a.p.s = (char *)dev->dname;
  494.         plist[3].value.a.size = -1;
  495.         pi = &plist[4];
  496.             /* resolution array */
  497.         plist[0].value.a.p.v = pi;
  498.         pi[0].value.f = dev->x_pixels_per_inch;
  499.         pi[1].value.f = dev->y_pixels_per_inch;
  500.         pi += 2;
  501.             /* width/height array */
  502.         plist[1].value.a.p.v = pi;
  503.         pi[0].value.i = dev->width;
  504.         pi[1].value.i = dev->height;
  505.         pi += 2;
  506.             /* matrix */
  507.         plist[2].value.a.p.v = pi;
  508.         (*dev->procs->get_initial_matrix)(dev, &mat);
  509.         pi[0].value.f = mat.xx;
  510.         pi[1].value.f = mat.xy;
  511.         pi[2].value.f = mat.yx;
  512.         pi[3].value.f = mat.yy;
  513.         pi[4].value.f = mat.tx;
  514.         pi[5].value.f = mat.ty;
  515.         pi += 6;
  516.        }
  517.     return sizeof(props_std) / sizeof(gs_prop_item);
  518. }
  519.  
  520. /* Set standard properties. */
  521. /* Note that if the device is open, it will be closed and reopened. */
  522. /****** DOESN'T FIND ALL GSTATES THAT REFERENCE THE DEVICE. ******/
  523. int
  524. gx_default_put_props(gx_device *dev, gs_prop_item *plist, int count)
  525. {    gs_prop_item *known[2];
  526.     int reopen;
  527.     int code = 0;
  528.     gx_device temp_dev;
  529.     props_extract(plist, count, props_std, 2, known, 1);
  530.     temp_dev = *dev;
  531.     if ( known[1] != 0 )
  532.        {    if ( known[1]->value.a.size != 2 )
  533.             known[1]->status = pv_typecheck,
  534.             code = gs_error_typecheck;
  535.         else
  536.            {    gs_prop_item *ap = known[1]->value.a.p.v;
  537.             if ( ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
  538.                  ap[1].value.i <= 0 || ap[1].value.i > 0x7fff
  539.                )
  540.                 known[1]->status = pv_rangecheck,
  541.                 code = gs_error_rangecheck;
  542.             else
  543.                {    temp_dev.width = ap[0].value.i;
  544.                 temp_dev.height = ap[1].value.i;
  545.                }
  546.            }
  547.        }
  548.     if ( known[0] != 0 )
  549.        {    if ( known[0]->value.a.size != 2 )
  550.             known[0]->status = pv_typecheck,
  551.             code = gs_error_typecheck;
  552.         else
  553.            {    gs_prop_item *ap = known[0]->value.a.p.v;
  554.             if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
  555.                 known[0]->status = pv_rangecheck,
  556.                 code = gs_error_rangecheck;
  557.             else
  558.                {    temp_dev.x_pixels_per_inch = ap[0].value.f;
  559.                 temp_dev.y_pixels_per_inch = ap[1].value.f;
  560.                }
  561.            }
  562.        }
  563.     if ( code < 0 )
  564.         return_error(code);
  565.     reopen = dev->is_open && (known[0] != 0 || known[1] != 0);
  566.     if ( reopen )
  567.         if ( (code = gs_closedevice(dev)) < 0 ) return code;
  568.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  569.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  570.     dev->width = temp_dev.width;
  571.     dev->height = temp_dev.height;
  572.     if ( reopen )
  573.     {    if ( (code = (*dev->procs->open_device)(dev)) < 0 )
  574.             return_error(code);
  575.         dev->is_open = 1;
  576.     }
  577.     return 0;
  578. }
  579.